package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Adapter.MyReservationAdapter;
import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import Entity.Inf;
import Entity.Reservations;

public class MyReservationFragment extends MyFragment {
    private static MyReservationAdapter myreservationAdapter;
    public static Handler myHandler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                myreservationAdapter.notifyDataSetChanged();
            } else if (msg.what == 1) {
                myreservationAdapter.reservations.get((int) msg.obj).AppointmentCheckStatus = 3;
                myreservationAdapter.notifyItemChanged((int) msg.obj);
                Toast.makeText(Inf.context, "取消成功", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_myreservation, null);
        RecyclerView recyclerView = view.findViewById(R.id.my_reservations);
        recyclerView.setLayoutManager(new LinearLayoutManager(Inf.context));
        myreservationAdapter = new MyReservationAdapter();
        myreservationAdapter.setOnItemClickListener(new MyReservationAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String appointmentNumber, boolean st, int position) {
                if (st) showSimpleAlertDialog(appointmentNumber, position);
            }
        });
        recyclerView.setAdapter(myreservationAdapter);
        view.findViewById(R.id.myr_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(Inf.centerFragment);
            }
        });
        return view;
    }

    public void refresh() {
        new MyReservationThread().start();
    }

    static class MyReservationThread extends Thread {
        @Override
        public void run() {
            Reservations res = GetResult.QueryMyReservation(Inf.stuInf.StudentAccount);
            if (res != null) {
                myreservationAdapter.reservations = res.reservations;
                myHandler.sendEmptyMessage(0);
            }
        }
    }

    private static void showSimpleAlertDialog(String appointmentNumber, int position) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(Inf.context);
        dialog.setTitle("是否取消预约:");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int res = GetResult.CancelGR(appointmentNumber);
                        if (res != 0) {
                            Message message = new Message();
                            message.what = 1;
                            message.obj = position;
                            myHandler.sendMessage(message);
                        }
                    }
                }).start();
            }
        });
        dialog.setNegativeButton("关闭", null);
        dialog.show();
    }
}
